package Interfaces;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

import Auxiliary.Sequence_Value;
import Boundary.UpdateProject;
import DataView.Action;
import DataView.Expr;
import DataView.Value;
import Store.Row;
import Visualization.StateTableVis;

public class SM_examinate 
{
	JPanel container;
	JPanel centerPanel;
	UpdateProject updateProject;
	StateTableVis ST;
	JComboBox[] b;
	
	public SM_examinate(UpdateProject upd)
	{
		updateProject=upd;
		ST= updateProject.main.OPEN_PRJ.stateTable.vis();
	}

	public JPanel examinate(Row source, Expr cond, Action act) 
	{
		container = new JPanel();
		container.setLayout(new BorderLayout());
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,4));
		JButton next = new JButton("Done");
		next.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				Sequence_Value values = new Sequence_Value();
				for(JComboBox cb : b)
					values.add(new Value(cb.getSelectedItem().toString()));
				if(updateProject.main.OPEN_PRJ.stateTable.findRow(values).getName().compareTo("impossible")==0)
					JOptionPane.showMessageDialog(container, "You choose an impossible target State! Check the state Table when done", "Error", JOptionPane.WARNING_MESSAGE);
				else
				{
					updateProject.answer(true, updateProject.main.OPEN_PRJ.stateTable.findRow(values));
					container.removeAll();
					container.setVisible(false);
				}
			}
		});
		JButton no = new JButton("Skip");
		no.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				container.removeAll();
				container.setVisible(false);
				updateProject.answer(false, null);
			}
		});
		buttonPanel.add(new JLabel(""));
		buttonPanel.add(next);
		buttonPanel.add(no);
		buttonPanel.add(new JLabel(""));
		
		container.add(getHead(source, cond, act) , BorderLayout.NORTH);
		container.add(getTarget(source), BorderLayout.CENTER);
		container.add(buttonPanel, BorderLayout.SOUTH);
		
		return container;
	}

	private JPanel getHead(Row source, Expr cond, Action act) 
	{
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		JLabel l = new JLabel("Select the target for this transition if possible", JLabel.CENTER);
		l.setFont(new Font("Tahoma", Font.BOLD, 20));
		panel.add(l, BorderLayout.NORTH);
		
		JPanel tabella = new JPanel();
		DefaultTableModel modelloRicerca = new DefaultTableModel();
		int i=0;
		for (@SuppressWarnings("unused") Value v : source.getValues().getAll()) 
			modelloRicerca.addColumn(ST.getUsedSo().get(i++).getName());
		modelloRicerca.addColumn("State");
		JTable tab = new JTable(modelloRicerca){
			private static final long serialVersionUID = 1L;
		public boolean isCellEditable(int rowIndex, int vColIndex){return false;}};
		tab.setAutoCreateColumnsFromModel(false);								
		tab.getTableHeader().setReorderingAllowed(false);
		tab.setFocusable(false);
		tab.setFillsViewportHeight(true);
		modelloRicerca.setRowCount(1);		
		for (int j=0; j<source.getValues().size(); j++)
			modelloRicerca.setValueAt(source.getValues().get(j).getName(), 0, j);
		modelloRicerca.setValueAt(source.getName(), 0, source.getValues().size());
		tabella.setLayout(new BorderLayout());
		tabella.add(tab.getTableHeader(), BorderLayout.NORTH);
		tabella.add(tab, BorderLayout.CENTER);
		JPanel tabella_aux =new JPanel();
		tabella_aux.add(new JLabel("Source State :"), BorderLayout.WEST);
		tabella_aux.add(tabella, BorderLayout.CENTER);
		panel.add(tabella_aux, BorderLayout.CENTER);
		
		JPanel panelCR = new JPanel();
		panelCR.setLayout(new GridLayout(1,2));
		JPanel aux1 = new JPanel();
		JPanel aux2 = new JPanel();
		aux1.setLayout(new BorderLayout());
		aux2.setLayout(new BorderLayout());
		aux1.add(new JLabel("Condition"), BorderLayout.NORTH);
		aux2.add(new JLabel("Reaction"), BorderLayout.NORTH);
		JTextArea c = new JTextArea(cond.getDescription());
		c.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		c.setLineWrap(true);
		c.setEditable(false);
		JTextArea r = new JTextArea(act.getDescription());
		r.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		r.setLineWrap(true);
		r.setEditable(false);
		aux1.add(c, BorderLayout.CENTER);
		aux2.add(r, BorderLayout.CENTER);
		panelCR.add(aux1);
		panelCR.add(aux2);
		panel.add(panelCR, BorderLayout.SOUTH);
		
		return panel;
	}

	public JPanel getTarget(Row source)
	{
		JPanel tabella = new JPanel();
		JPanel aux= new JPanel();
		aux.setLayout(new GridLayout(1,source.getValues().size()));
		DefaultTableModel modelloRicerca = new DefaultTableModel();
		b= new JComboBox[source.getValues().size()];
		int i=0;
		for (@SuppressWarnings("unused") Value v : source.getValues().getAll()) 
		{
			modelloRicerca.addColumn(ST.getUsedSo().get(i).getName());
			
			b[i]=new JComboBox();
			b[i].addItem("true");
			b[i].addItem("false");
			aux.add(b[i]);
			if(source.getValues().get(i).getName().compareTo("false")==0)
				b[i].setSelectedIndex(1);
			i++;
		}
		JTable tab = new JTable(modelloRicerca)
		{
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int rowIndex, int vColIndex){return false;}};
		tab.setAutoCreateColumnsFromModel(false);								
		tab.getTableHeader().setReorderingAllowed(false);
		tab.setFocusable(false);
		tab.setFillsViewportHeight(true);
		tabella.setLayout(new BorderLayout());
		tabella.add(tab.getTableHeader(), BorderLayout.NORTH);
		tabella.add(aux, BorderLayout.CENTER);
		JPanel tabella_aux =new JPanel();
		tabella_aux.add(new JLabel("Select target :"), BorderLayout.WEST);
		tabella_aux.add(tabella, BorderLayout.CENTER);
		return tabella_aux;
	}
}

